import { saveToken } from 'woyun-base'
import childRouters from './children'

Vue.use(VueRouter)

const router = new VueRouter({
    mode: 'history',
    base: '/sport',
    routes: [
        {
            path: '/index',
            name:'Index',
            component: () => import('@/components/parent/parent.vue'),
            props: (route) => (route.query),
            children: childRouters
        }
    ],
    scrollBehavior (to, from, savedPosition) {
        // 解决A页面可滑动，跳转到可滑动的B页面会默认定位到A页面滚动位置
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                if (to.hash) {
                    resolve({ selector: to.hash })
                } else {
                    if (savedPosition) {
                        resolve(savedPosition) //return savedPosition
                    } else {
                        resolve({ x: 0, y: 0 })
                    }
                }
            }, 100)
        })
    }
})
// 默认存储路由有token存储到本地，设置title
router.beforeEach((to, from, next) => {
    let accessToken = to.query.access_token
    if (accessToken && accessToken != null) {
        saveToken(accessToken)
    }
    if(to.meta.title) {
        document.title = to.meta.title
    }
    next()
})
export default router